using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Slobby.Client.UI.Controls {
    public partial class ChatBox : UserControl {
        private bool BoxEmpty = true;
        
        public ChatBox() {
            InitializeComponent();
        }

        private string FormatLine(string username, string message) {
            return string.Format("{0:HH}:{0:mm}:{0:ss} {1,10} {2}",
                DateTime.Now,
                "<" + username + ">",
                message);
        }

        public void AddLine (string username, string message) {
            if (!BoxEmpty)
                chat.AppendText(Environment.NewLine);
            chat.AppendText(FormatLine(username, message));
            BoxEmpty = false;
            if (!chat.Focused) {
                chat.SelectionStart = chat.Text.Length;
                chat.ScrollToCaret();
            }
        }

        public event Action<string> ChatMessage;

        private void CheckReturn(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Return) {
                e.SuppressKeyPress = true;
                string text = typeingBox.Text;
                ChatMessage(text);
                typeingBox.Text = string.Empty;
            }
        }
    }
}